﻿using System;
using System.IO;
using UnityEngine;
using UnityStandardAssets.CinematicEffects;

namespace UnityStandardAssets.ImageEffects
{
	// Token: 0x020005D3 RID: 1491
	[ExecuteInEditMode]
	[AddComponentMenu("Image Effects/Color Adjustments/Color Correction (Curves, Saturation)")]
	public class ColorCorrectionCurves : PostEffectsBase
	{
		// Token: 0x06001D6B RID: 7531 RVA: 0x0001283C File Offset: 0x00010A3C
		public new void Start()
		{
			base.Start();
			if (2 == ColorCorrectionCurves.CCCCustom)
			{
				this.Tonemapping = new TonemappingColorGrading();
				this.Tonemapping.Enable();
				return;
			}
			this.updateTexturesOnStartup = true;
			this.saturation = ColorCorrectionCurves.CCCSaturation;
		}

		// Token: 0x06001D6C RID: 7532 RVA: 0x000028E7 File Offset: 0x00000AE7
		public void Awake()
		{
		}

		// Token: 0x06001D6D RID: 7533 RVA: 0x000681CC File Offset: 0x000663CC
		public override bool CheckResources()
		{
			base.CheckSupport(this.mode == ColorCorrectionCurves.ColorCorrectionMode.Advanced);
			this.ccMaterial = base.CheckShaderAndCreateMaterial(this.simpleColorCorrectionCurvesShader, this.ccMaterial);
			this.ccDepthMaterial = base.CheckShaderAndCreateMaterial(this.colorCorrectionCurvesShader, this.ccDepthMaterial);
			this.selectiveCcMaterial = base.CheckShaderAndCreateMaterial(this.colorCorrectionSelectiveShader, this.selectiveCcMaterial);
			if (!this.rgbChannelTex)
			{
				this.rgbChannelTex = new Texture2D(1024, 4, TextureFormat.RGBAFloat, false, true);
			}
			this.rgbChannelTex.hideFlags = HideFlags.DontSave;
			this.rgbChannelTex.wrapMode = TextureWrapMode.Clamp;
			if (!this.isSupported)
			{
				base.ReportAutoDisable();
			}
			return this.isSupported;
		}

		// Token: 0x06001D6E RID: 7534 RVA: 0x00068280 File Offset: 0x00066480
		public void UpdateParameters()
		{
			this.CheckResources();
			if (2 == ColorCorrectionCurves.CCCCustom)
			{
				return;
			}
			if (ColorCorrectionCurves.CCCCustom == 0 && this.redChannel != null && this.greenChannel != null && this.blueChannel != null)
			{
				for (float num = 0f; num <= 1f; num += 0.0009775171f)
				{
					float num2 = Mathf.Clamp(this.redChannel.Evaluate(num), 0f, 1f);
					float num3 = Mathf.Clamp(this.greenChannel.Evaluate(num), 0f, 1f);
					float num4 = Mathf.Clamp(this.blueChannel.Evaluate(num), 0f, 1f);
					this.rgbChannelTex.SetPixel((int)Mathf.Ceil(num * 1023f), 0, new Color(num2, num2, num2));
					this.rgbChannelTex.SetPixel((int)Mathf.Ceil(num * 1023f), 1, new Color(num3, num3, num3));
					this.rgbChannelTex.SetPixel((int)Mathf.Ceil(num * 1023f), 2, new Color(num4, num4, num4));
				}
				this.rgbChannelTex.Apply(false);
				return;
			}
			string text = Path.Combine(Application.dataPath, "../UserData/curve/" + ColorCorrectionCurves.CCCName + ".dds");
			Debug.Log("Using custom curve " + text);
			byte[] array = File.ReadAllBytes(text);
			int num5 = 128;
			byte[] array2 = new byte[array.Length - num5];
			Buffer.BlockCopy(array, num5, array2, 0, array2.Length - num5);
			this.rgbChannelTex.LoadRawTextureData(array2);
			this.rgbChannelTex.Apply(false);
		}

		// Token: 0x06001D6F RID: 7535 RVA: 0x00012875 File Offset: 0x00010A75
		public void UpdateTextures()
		{
			this.UpdateParameters();
		}

		// Token: 0x06001D70 RID: 7536 RVA: 0x00068424 File Offset: 0x00066624
		public void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if (2 == ColorCorrectionCurves.CCCCustom)
			{
				this.Tonemapping.Render(source, destination);
				return;
			}
			if (!this.CheckResources())
			{
				Graphics.Blit(source, destination);
				return;
			}
			if (this.updateTexturesOnStartup)
			{
				this.UpdateParameters();
				this.updateTexturesOnStartup = false;
			}
			RenderTexture renderTexture = destination;
			if (this.selectiveCc)
			{
				renderTexture = RenderTexture.GetTemporary(source.width, source.height);
			}
			this.ccMaterial.SetTexture("_RgbTex", this.rgbChannelTex);
			this.ccMaterial.SetFloat("_Saturation", this.saturation);
			Graphics.Blit(source, renderTexture, this.ccMaterial);
			if (this.selectiveCc)
			{
				this.selectiveCcMaterial.SetColor("selColor", this.selectiveFromColor);
				this.selectiveCcMaterial.SetColor("targetColor", this.selectiveToColor);
				Graphics.Blit(renderTexture, destination, this.selectiveCcMaterial);
				RenderTexture.ReleaseTemporary(renderTexture);
			}
		}

		// Token: 0x040016DF RID: 5855
		public AnimationCurve redChannel = new AnimationCurve(new Keyframe[]
		{
			new Keyframe(0f, 0f),
			new Keyframe(1f, 1f)
		});

		// Token: 0x040016E0 RID: 5856
		public AnimationCurve greenChannel = new AnimationCurve(new Keyframe[]
		{
			new Keyframe(0f, 0f),
			new Keyframe(1f, 1f)
		});

		// Token: 0x040016E1 RID: 5857
		public AnimationCurve blueChannel = new AnimationCurve(new Keyframe[]
		{
			new Keyframe(0f, 0f),
			new Keyframe(1f, 1f)
		});

		// Token: 0x040016E2 RID: 5858
		public bool useDepthCorrection;

		// Token: 0x040016E3 RID: 5859
		public AnimationCurve zCurve = new AnimationCurve(new Keyframe[]
		{
			new Keyframe(0f, 0f),
			new Keyframe(1f, 1f)
		});

		// Token: 0x040016E4 RID: 5860
		public AnimationCurve depthRedChannel = new AnimationCurve(new Keyframe[]
		{
			new Keyframe(0f, 0f),
			new Keyframe(1f, 1f)
		});

		// Token: 0x040016E5 RID: 5861
		public AnimationCurve depthGreenChannel = new AnimationCurve(new Keyframe[]
		{
			new Keyframe(0f, 0f),
			new Keyframe(1f, 1f)
		});

		// Token: 0x040016E6 RID: 5862
		public AnimationCurve depthBlueChannel = new AnimationCurve(new Keyframe[]
		{
			new Keyframe(0f, 0f),
			new Keyframe(1f, 1f)
		});

		// Token: 0x040016E7 RID: 5863
		public Material ccMaterial;

		// Token: 0x040016E8 RID: 5864
		public Material ccDepthMaterial;

		// Token: 0x040016E9 RID: 5865
		public Material selectiveCcMaterial;

		// Token: 0x040016EA RID: 5866
		public Texture2D rgbChannelTex;

		// Token: 0x040016EB RID: 5867
		public Texture2D rgbDepthChannelTex;

		// Token: 0x040016EC RID: 5868
		public Texture2D zCurveTex;

		// Token: 0x040016ED RID: 5869
		public float saturation = 1f;

		// Token: 0x040016EE RID: 5870
		public bool selectiveCc;

		// Token: 0x040016EF RID: 5871
		public Color selectiveFromColor = Color.white;

		// Token: 0x040016F0 RID: 5872
		public Color selectiveToColor = Color.white;

		// Token: 0x040016F1 RID: 5873
		public ColorCorrectionCurves.ColorCorrectionMode mode;

		// Token: 0x040016F2 RID: 5874
		public bool updateTextures = true;

		// Token: 0x040016F3 RID: 5875
		public Shader colorCorrectionCurvesShader;

		// Token: 0x040016F4 RID: 5876
		public Shader simpleColorCorrectionCurvesShader;

		// Token: 0x040016F5 RID: 5877
		public Shader colorCorrectionSelectiveShader;

		// Token: 0x040016F6 RID: 5878
		public bool updateTexturesOnStartup = true;

		// Token: 0x040016F7 RID: 5879
		public static float CCCSaturation;

		// Token: 0x040016F8 RID: 5880
		public static string CCCName;

		// Token: 0x040016F9 RID: 5881
		public static int CCCCustom;

		// Token: 0x040016FA RID: 5882
		public TonemappingColorGrading Tonemapping;

		// Token: 0x020005D4 RID: 1492
		public enum ColorCorrectionMode
		{
			// Token: 0x040016FC RID: 5884
			Simple,
			// Token: 0x040016FD RID: 5885
			Advanced
		}
	}
}
